var currentLocationId;
var markers = [];

Page({
	data: {
		activityContent: '',
		location: '',
		scale: 16,
		longitude: 120.388377,
		latitude: 30.310886,


		polygon: [{
			points: [{ //左上
				latitude: 30.310848,
				longitude: 120.381935,
			}, { //左下
				latitude: 30.306673,
				longitude: 120.381897,
			}, { //右下
				latitude: 30.306674,
				longitude: 120.395098,
			}, { //右上
				latitude: 30.310819,
				longitude: 120.397142,
			}],
			color: '#70DBDBDD',
			width: 3,
		}, {
			points: [{ //左上
				latitude: 30.313801,
				longitude: 120.387454,
			}, { //左下
				latitude: 30.311034,
				longitude: 120.387497,
			}, { //右下
				latitude: 30.311002,
				longitude: 120.397246,
			}, { //右上
				latitude: 30.313772,
				longitude: 120.398544,
			}],
			color: '#70DBDBDD',
			width: 3,
		}, {
			points: [{ //左上
				latitude: 30.314211,
				longitude: 120.377856,
			}, { //左下
				latitude: 30.311057,
				longitude: 120.376611,
			}, { //右下
				latitude: 30.311057,
				longitude: 120.381718,
			}, { //右上
				latitude: 30.31378,
				longitude: 120.381772,
			}],
			color: '#70DBDBDD',
			width: 3,
		}],

		//中心位置
		includePoints: [{
			latitude: 30.308648,
			longitude: 120.3886,
		}],

		//左下角控制
		controls: [{
			id: 5,
			iconPath: '/image/loc.png',
			position: {
				left: 15,
				top: 650 - 20,
				width: 40,
				height: 40,
			},
			clickable: true
		}]
	},

	onReady(e) {
		// 使用 my.createMapContext 获取 map 上下文
		this.mapCtx = my.createMapContext('map')
	},

	moveToLocation() {
		this.mapCtx.moveToLocation()
	},

	regionchange(e) {
		console.log('regionchange', e);
	},

	//创建markers
	onLoad: function(option) {
		my.request({
			url: 'https://www.zjsu.club/club/map/',
			method: 'GET',
			success: (res) => {
				var data = res.data.data;
				console.log(data)
				for (var i = 0; i < data.length; i++) {
					var info = {
						iconPath: "/image/timg.png",
						id: data[i].locationId,
						latitude: data[i].latitude,
						longitude: data[i].longitude,
						width: 30,
						height: 30,
						name: data[i].name,
						label: {
							content: data[i].name,
							color: "#000000",
							fontSize: 18,
							borderRadius: "50", 
							bgColor: "#ffffff",
							padding: 15,
						},
					};
					markers.push(info);
				}

				this.setData({
					markers: markers,
				})
			},
			fail: (err) => {
				console.log('error', err);
			},
		});
	},

	//点击markers 弹出菜单界面
	markertap(e) {
		console.log(e)
		currentLocationId = '';
		this.setData({
			activityContent: '',
			location: '',
		});

		my.request({
			url: 'https://www.zjsu.club/club/info/?locationId=' + e.markerId,
			method: 'GET',
			success: (res) => {
				console.log(res.data)
				// for (var i = 0; i < res.data.length; i++) {
				// 	this.data.location = res.data[i].location
				// 	this.data.activityContent = res.data[i].desc
				// 	currentLocationId = e.markerId
				// }
				this.data.location = res.data[0].location
				this.data.activityContent = res.data[0].desc
				currentLocationId = e.markerId

				this.setData({
					activityContent: this.data.activityContent,
					location: this.data.location,
				})
			},
		});
		// console.log('marker tap', e);
		//底部弹出菜单
		this.setData({
			showBottom: true,
		});
	},

	//跳转到活动详细页面
	opendetail(e) {
		const name = e.currentTarget.dataset.name
		let url = "/pages/activity_info/activity_info?locationId=" + 
		currentLocationId + "&name=" + name
		my.navigateTo({
			url,
		})
	},

	controltap(e) {
		//console.log('control tap', e);
		this.mapCtx.moveToLocation()
	},

	tap() {
		console.log('tap:');
	},

	//支持地图不接受手势事件, isGestureEnable为1 表示支持，为0表示不支持
	gestureEnable() {
		this.mapCtx.gestureEnable({
			isGestureEnable: 1
		});
	},
	//地图是否显示比例尺, showsScale 为1表示显示，为0表示不显示
	showsScale() {
		this.mapCtx.showsScale({
			isShowsScale: 1
		});
	},
	//地图是否显示指南针, showsCompass 为1表示显示，为0表示不显示
	showsCompass() {
		this.mapCtx.showsCompass({
			isShowsCompass: 1
		});
	},

	//弹出菜单关闭
	onPopupClose() {
		this.setData({
			showBottom: false,
		});
	},

})
